package com.techm.smptest.network;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Request {

	Document doc;

	Element entry, content, payload;

	public Request() {
		try {

			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

			doc = docBuilder.newDocument();
			entry = doc.createElement("entry");

			/*
			 * set attribute to entry element
			 */
			Attr attr = doc.createAttribute("xmlns");
			attr.setValue("http://www.w3.org/2005/Atom");
			entry.setAttributeNode(attr);

			attr = doc.createAttribute("xmlns:m");
			attr.setValue("http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
			entry.setAttributeNode(attr);

			attr = doc.createAttribute("xmlns:d");
			attr.setValue("http://schemas.microsoft.com/ado/2007/08/dataservices");
			entry.setAttributeNode(attr);

			doc.appendChild(entry);

			addContentNode();

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		}
	}

	private void addContentNode() {
		content = doc.createElement("content");

		Attr attr = doc.createAttribute("type");
		attr.setValue("application/xml");
		content.setAttributeNode(attr);

		entry.appendChild(content);
	}

	public void addPayloadRegister(String devType) {
		payload = doc.createElement("m:properties");
		content.appendChild(payload);

		Element deviceType = doc.createElement("d:DeviceType");
		deviceType.appendChild(doc.createTextNode(devType));
		payload.appendChild(deviceType);
	}

	public void addPayloadLogin(String username, String password) {
		payload = doc.createElement("login");
		content.appendChild(payload);

		Element elements = doc.createElement("elements");

		Attr attr = doc.createAttribute("user");
		attr.setValue(username);
		elements.setAttributeNode(attr);

		attr = doc.createAttribute("password");
		attr.setValue(password);
		elements.setAttributeNode(attr);

		payload.appendChild(elements);
	}

	public String getXMLInString() {
		try {
			StringWriter sw = new StringWriter();
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			transformer
					.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
			transformer.setOutputProperty(OutputKeys.METHOD, "xml");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

			transformer.transform(new DOMSource(doc), new StreamResult(sw));
			return sw.toString();
		} catch (Exception ex) {
			throw new RuntimeException("Error converting to String", ex);
		}
	}

}